/* global window */
import modelExtend from 'dva-model-extend'
import {create, remove, update} from './services/department'
import * as deptService from './services/departments'
import * as employeeService from '../employee/services/employees'
import {pageModel} from 'utils/model'
import {queryArray} from 'utils'

const {query} = deptService

const setParent = (list) => {
  let parent
  list.map(p => {
    if (p.parentId && p.parentId !== (parent ? parent.id : -1)) {
      parent = queryArray(list, p.parentId, 'id')
    }

    if (parent && p.parentId && p.parentId === parent.id) {
      p['parent'] = {
        id: parent.id,
        sn: parent.sn,
        name: parent.name,
      }
    }
  })
  list.reverse()
}

export default modelExtend(pageModel, {
  namespace: 'department',

  state: {
    currentItem: {},
    modalVisible: false,
    modalType: 'create',
    selectedRowKeys: [],
    empDataSource: [],
    activeKey: "group_list",
  },

  subscriptions: {
    setup ({dispatch, history}) {
      history.listen((location) => {
        if (location.pathname === '/department') {
          const payload = location.query || {current: 1, size: 10}
          dispatch({
            type: 'query',
            payload,
          })
          dispatch({
            type: 'searchEmp',
            payload: {key: "any"},
          })
        }
      })
    },
  },

  effects: {

    * query ({payload = {}}, {call, put}) {
      const response = yield call(query, payload)
      const {data} = response
      if (data) {
        setParent(data)
        yield put({
          type: 'querySuccess',
          payload: {
            list: data,
          },
        })
      }
    },

    * searchEmp ({payload = {}}, {call, put}) {
      const response = yield call(employeeService.searchEmp, payload)
      const {data} = response
      if (response && data) {
        yield put({
          type: 'updateState',
          payload: {
            empDataSource: data,
          },
        })
      }
    },

    * delete ({payload}, {call, put, select}) {
      const data = yield call(remove, payload)
      const {selectedRowKeys} = yield select(_ => _.department)
      if (data.code === 0) {
        yield put({type: 'updateState', payload: {selectedRowKeys: selectedRowKeys.filter(_ => _ !== payload)}})
      } else {
        throw data
      }
    },

    * multiDelete ({payload}, {call, put}) {
      const data = yield call(deptService.remove, payload)
      if (data.code === 0) {
        yield put({type: 'updateState', payload: {selectedRowKeys: []}})
      } else {
        throw data
      }
    },

    * create ({payload}, {call, put}) {
      const data = yield call(create, payload)
      if (data.code === 0) {
        yield put({type: 'hideModal'})
      } else {
        throw data
      }
    },

    * update ({payload}, {select, call, put}) {
      const id = yield select(({department}) => {
        if (payload.id) {
          return payload.id
        }
        return department.currentItem.id
      })

      const newDept = {...payload, id}
      const data = yield call(update, newDept)
      if (data.code === 0) {
        yield put({type: 'hideModal'})
      } else {
        throw data
      }
    },

  },

  reducers: {

    showModal (state, {payload}) {
      return {...state, ...payload, modalVisible: true}
    },

    hideModal (state) {
      return {...state, modalVisible: false}
    },

  },
})
